1 import java.util.*;
2
3 public class MCommandQueue {
4
5 ObjectMonitor myLock;
6 Vector queue;
7 int queueSize;
8
9 MCommandQueue() {
10 myLock = new ObjectMonitor();
11 queue = new Vector();
12 queueSize = 0;
13 }
14
15 public MCommand get() {
16 MCommand c = null;
17 // blocks until a message arrives
18 myLock.lock(true);
19 if (!queue.isEmpty()) {
20 c = (MCommand) queue.elementAt(0);
21 queue.removeElementAt(0);
22 queueSize--;
23 }
24 myLock.lock(false);
25 return c;
26 }
27
28 public void put(MCommand c) {
29 myLock.lock(true);
30 queue.addElement(c);
31 queueSize++;
32 myLock.lock(false);
33 }
34
35 public boolean has() {
36 if (queueSize > 0) return true;
37 else return false;
38 }
39
40 }
|